<template>
	<view class="detail">
		<view class="title">{{detail.title}}</view>
		<view class="info">
			<view class="author">编辑：{{detail.author}}</view>
			<view class="time">发布日期：{{detail.posttime}}</view>
		</view>
		<view class="content">
			<rich-text :nodes="detail.content"></rich-text>
		</view>
		<view class="description">
			声明：本站的内容均采集与腾讯新闻，如果侵权请联系管理（xxxxxxxx@qq.com）进行整改删除，本站进行了内容采集不代表本站及作者观点，若有侵犯请及时联系管理员，谢谢您的支持。
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				options: null,
				detail: {}
			};
		},
		onLoad(e) {
			// console.log(e);
			this.options = e
			this.getNewsDetail(e)
		},
		methods: {
			// getNewsDetail({cid,id}) 
			getNewsDetail() {
				uni.showLoading({})
				uni.request({
					// url: `https://ku.qingnian8.com/dataApi/news/detail.php?cid=${cid}&id=${id}`,
					url: `https://ku.qingnian8.com/dataApi/news/detail.php`,
					data: this.options,
					success: (res) => {
						console.log(res);
						res.data.posttime = new Date(res.data.posttime * 1).toLocaleString()
						this.detail = res.data

						this.saveHistory()

						uni.setNavigationBarTitle({
							title: this.detail.title
						})
					},
					complete: () => {
						uni.hideLoading({})
					}
				})
			},
			saveHistory() {
				let newsItem = {
					id: this.detail.id,
					classid: this.detail.classid,
					picurl: this.detail.picurl,
					title: this.detail.title,
					looktime: new Date().toLocaleString()
				}
				let newsHistory = uni.getStorageSync("newsHistory") || []
				let newsHistoryIndex = newsHistory.findIndex(item => item.id == this.detail.id)
				if (newsHistoryIndex > -1) {
					newsHistory.splice(newsHistoryIndex, 1)
				}
				newsHistory.unshift(newsItem)
				newsHistory = newsHistory.slice(0, 10)
				uni.setStorageSync('newsHistory', newsHistory)
			}
		}
	}
</script>

<style lang="scss">
	.detail {
		padding: 30rpx;

		.title {
			font-size: 46rpx;
			color: #333;
		}

		.info {
			background: #F6F6F6;
			padding: 20rpx;
			font-size: 25rpx;
			color: #666;
			display: flex;
			justify-content: space-between;
			margin: 40rpx 0;
		}

		.content {
			padding-bottom: 50rpx;

			img {
				width: 100%;
				height: 100%;
			}
		}

		.description {
			background: #FEF0F0;
			font-size: 26rpx;
			padding: 20rpx;
			color: #F89898;
			line-height: 1.8em;
		}
	}
</style>